﻿-- Author      : Thorwald Odin
-- Create Date : 12/7/2013 1:46:25 PM
--[[
--------------------------------------------------------------------------------
MoneyInput Widget
--------------------------------------------------------------------------------
--]]

local Type, Version = 'AceGUIWidget-MoneyInput', 1;
local AceGUI = LibStub and LibStub('AceGUI-3.0', true);
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then
	return; -- Newer version already registered
end

--[[
	Lua APIs
--]]

local tostring, pairs = tostring, pairs;
local _G = _G;

--[[
	WoW APIs
--]]

local CreateFrame, UIParent = CreateFrame, UIParent;

--[[
	WoW Constants
--]]
local COPPER_PER_GOLD, COPPER_PER_SILVER = COPPER_PER_GOLD, COPPER_PER_SILVER;

--[[
	Scripts
--]]

local function Control_OnEnter(frame)
	frame.obj:Fire('OnEnter');
end

local function Control_OnLeave(frame)
	frame.obj:Fire('OnLeave');
end

local function Control_OnEscapePressed(frame)
	AceGUI:ClearFocus();
	frame:ClearFocus();
end

local function Control_OnEnterPressed(frame)
	local self = frame.obj;
	local gold = self.Gold:GetNumber();
	local silver = self.Silver:GetNumber();
	local copper = self.Copper:GetNumber();
	local money = (gold * COPPER_PER_GOLD) + (silver * COPPER_PER_SILVER) + copper;
	self:Fire('OnEnterPressed', money);
end

local function Control_OnTabPressed(frame)
	if IsShiftKeyDown() then
		frame.PrevFocus:SetFocus();
	else
		frame.NextFocus:SetFocus();
	end
end

local function Control_GainFocus(frame)
	frame:HighlightText();
end

local function Control_LoseFocus(frame)
	frame:HighlightText(0, 0);
end

--[[
	Methods
--]]

local Control_Methods = {
	OnAcquire = function(self)
		self.frame:SetWidth(200);
		self:SetDisabled(false);
		self:SetLabel();
		self:SetText('');
		self.Gold:SetMaxLetters(0);
		self.Gold:SetNumeric(true);
		self.Silver:SetMaxLetters(2);
		self.Silver:SetNumeric(true);
		self.Copper:SetMaxLetters(2);
		self.Copper:SetNumeric(true);
	end,
	SetDisabled = function(self, disabled)
		self.Disabled = disabled;
		if disabled then
			self.Gold:EnableMouse(false);
			self.Gold:ClearFocus();
			self.Gold:SetTextColor(0.5, 0.5, 0.5);
			self.Silver:EnableMouse(false);
			self.Silver:ClearFocus();
			self.Silver:SetTextColor(0.5, 0.5, 0.5);
			self.Copper:EnableMouse(false);
			self.Copper:ClearFocus();
			self.Copper:SetTextColor(0.5, 0.5, 0.5);
			self.Label:SetTextColor(0.5, 0.5, 0.5);
		else
			self.Gold:EnableMouse(true);
			self.Gold:SetTextColor(1, 1, 1);
			self.Silver:EnableMouse(true);
			self.Silver:SetTextColor(1, 1, 1);
			self.Copper:EnableMouse(true);
			self.Copper:SetTextColor(1, 1, 1);
			self.Label:SetTextColor(1, 0.82, 0);
		end
	end,
	SetText = function(self, text)
		local money = tonumber(text) or 0;
		local gold = floor(money / (COPPER_PER_GOLD));
		local silver = floor((money - (gold * COPPER_PER_GOLD)) / COPPER_PER_SILVER);
		local copper = mod(money, COPPER_PER_SILVER);
		
		self.Gold:SetNumber(gold);
		self.Silver:SetNumber(silver);
		self.Copper:SetNumber(copper);
	end,
	SetLabel = function(self, text)
		if text and text ~= '' then
			self.Label:SetText(text);
			self.Label:Show();
			self:SetHeight(44);
			self.alignoffset = 30;
			self.Gold:SetPoint('TOPLEFT', self.frame, 'TOPLEFT', 7, -18);
		else
			self.Label:SetText('');
			self.Label:Hide();
			self:SetHeight(26);
			self.alignoffset = 12;
			self.Gold:SetPoint('TOPLEFT', self.frame, 'TOPLEFT', 7, 0);
		end
	end,
};

--[[
	Constructor
--]]

local function Constructor()
	local num = AceGUI:GetNextWidgetNum(Type);
	local frame = CreateFrame('Frame', nil, UIParent);
	frame:Hide();
	local tex = nil;
	
	local gold = CreateFrame('EditBox', 'AceGUI-3.0MoneyInputBox' .. num .. 'Gold', frame, 'InputBoxTemplate');
	gold:SetHeight(20);
	gold:SetWidth(55);
	gold:SetPoint('TOPLEFT');
	gold:SetMaxLetters(0);
	gold:SetNumeric(true);
	gold:SetAutoFocus(false);
	tex = gold:CreateTexture('', 'BACKGROUND');
	tex:SetTexture([[Interface\MoneyFrame\UI-MoneyIcons]]);
	tex:SetHeight(13);
	tex:SetWidth(13);
	tex:SetPoint('LEFT', gold, 'RIGHT', 0, 0);
	tex:SetTexCoord(0, 0.25, 0, 1);
	gold.Texture = tex;
	
	gold:SetScript('OnEnter', Control_OnEnter);
	gold:SetScript('OnLeave', Control_OnLeave);
	gold:SetScript('OnEscapePressed', Control_OnEscapePressed);
	gold:SetScript('OnEnterPressed', Control_OnEnterPressed);
	gold:SetScript('OnTabPressed', Control_OnTabPressed);
	gold:SetScript('OnEditFocusGained', Control_GainFocus);
	gold:SetScript('OnEditFocusLost', Control_LoseFocus);
	
	local silver = CreateFrame('EditBox', 'AceGUI-3.0MoneyInputBox' .. num .. 'Silver', frame, 'InputBoxTemplate');
	silver:SetHeight(20);
	silver:SetWidth(30);
	silver:SetPoint('TOPLEFT', gold, 'TOPRIGHT', 18, 0);
	silver:SetMaxLetters(2);
	silver:SetNumeric(true);
	silver:SetAutoFocus(false);
	tex = silver:CreateTexture('', 'BACKGROUND');
	tex:SetTexture([[Interface\MoneyFrame\UI-MoneyIcons]]);
	tex:SetHeight(13);
	tex:SetWidth(13);
	tex:SetPoint('LEFT', silver, 'RIGHT', 0, 0);
	tex:SetTexCoord(0.25, 0.5, 0, 1);
	silver.Texture = tex;
	
	silver:SetScript('OnEnter', Control_OnEnter);
	silver:SetScript('OnLeave', Control_OnLeave);
	silver:SetScript('OnEscapePressed', Control_OnEscapePressed);
	silver:SetScript('OnEnterPressed', Control_OnEnterPressed);
	silver:SetScript('OnTabPressed', Control_OnTabPressed);
	silver:SetScript('OnEditFocusGained', Control_GainFocus);
	silver:SetScript('OnEditFocusLost', Control_LoseFocus);
	
	local copper = CreateFrame('EditBox', 'AceGUI-3.0MoneyInputBox' .. num .. 'Copper', frame, 'InputBoxTemplate');
	copper:SetHeight(20);
	copper:SetWidth(30);
	copper:SetPoint('TOPLEFT', silver, 'TOPRIGHT', 18, 0);
	copper:SetMaxLetters(2);
	copper:SetNumeric(true);
	copper:SetAutoFocus(false);
	tex = copper:CreateTexture('', 'BACKGROUND');
	tex:SetTexture([[Interface\MoneyFrame\UI-MoneyIcons]]);
	tex:SetHeight(13);
	tex:SetWidth(13);
	tex:SetPoint('LEFT', copper, 'RIGHT', 0, 0);
	tex:SetTexCoord(0.5, 0.75, 0, 1);
	silver.Texture = tex;
	
	copper:SetScript('OnEnter', Control_OnEnter);
	copper:SetScript('OnLeave', Control_OnLeave);
	copper:SetScript('OnEscapePressed', Control_OnEscapePressed);
	copper:SetScript('OnEnterPressed', Control_OnEnterPressed);
	copper:SetScript('OnTabPressed', Control_OnTabPressed);
	copper:SetScript('OnEditFocusGained', Control_GainFocus);
	copper:SetScript('OnEditFocusLost', Control_LoseFocus);
	
	local label = frame:CreateFontString(nil, 'OVERLAY', 'GameFontNormalSmall');
	label:SetPoint('TOPLEFT', 0, -2);
	label:SetPoint('TOPRIGHT', 0, -2);
	label:SetJustifyH('LEFT');
	label:SetHeight(18);
	
	gold.NextFocus = silver;
	gold.PrevFocus = copper;
	silver.NextFocus = copper;
	silver.PrevFocus = gold;
	copper.NextFocus = gold;
	copper.PrevFocus = silver;
	
	local widget = {
		Gold = gold,
		Silver = silver,
		Copper = copper,
		Label = label,
		alignoffset = 30,
		frame = frame,
		type = Type,
	};
	
	for method, func in pairs(Control_Methods) do
		widget[method] = func;
	end
	
	frame.obj, gold.obj, silver.obj, copper.obj = widget, widget, widget, widget;
	
	return AceGUI:RegisterAsWidget(widget);
end

AceGUI:RegisterWidgetType(Type, Constructor, Version)


